package com.qdxwx.manage.controller.coupon;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.coupon.CouponService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.user.UserService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.coupon.Coupon;
import com.qdxwx.models.user.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 优惠卷种类信息Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/coupon")
public class CouponController extends BaseController {

    @Autowired
    private CouponService couponService;
    @Autowired
    private UserService userService;

    @GetMapping("/list.html")
    public String coupon(ModelMap mmap) {
        mmap.put("userCount", userService.count(new UserQuery()));
        return "coupon/coupon-list";
    }

    /**
     * 查询优惠卷种类信息列表
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        String title = request.getParameter("title");
        Integer useType = TKConvert.toInteger(request.getParameter("useType"), null);
        Coupon coupon = new Coupon();
        coupon.setTitle(TKString.isNotEmpty(title) ? TKString.queryKey(title) : null);
        coupon.setUseType(useType);
        List<Coupon> list = couponService.getList(coupon, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(couponService.count(coupon));
        return rspData;
    }


}
